package com.sinodata.bsm.cicp.finder.hp;

/**
 * HP Mem的发现程序
 * @author zhaoch
 * @version 1.0
 */
import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.util.CommandReader;

/**
*HP操作系统的内存发现程序,
*调用hp_memory -p 获取物理内存大小
*/

public class Mem extends HPFinder {

    private static final Logger logger = Logger.getLogger(Mem.class);
    private static String shell_1 = "hp_memory -p"; //获取物理内存大小
    private static String shell_2 = "getconf PAGE_SIZE"; //获取内存页大小
    private static int K = 1024;

    public static void main(String[] args) {

    }

    @SuppressWarnings("unchecked")
    @Override
    public List<String[]> find(String ip, Map params) {
        List<String[]> list = new ArrayList<String[]>();
        String[] memInfo = new String[5];
        list.add(memInfo);
        memInfo[0] = "0";
        memInfo[1] = "Memory";
        memInfo[2] = getHostName(ip) + "Memory";
        try {
            //内存页大小
            memInfo[3] = getSize(shell_2, ip) + "";
            //内存大小
            memInfo[4] = getSize(shell_1, ip) / K + "";
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private int getSize(String shell, String ip) throws Exception {
        BufferedReader reader = null;
        try {
            reader = CommandReader.getReader(ip, shell, 0, 0);
            String size = null;
            while (true) {
                size = reader.readLine();
                if (size == null) {
                    break;
                } else if ((size = size.trim()).equals("")) {
                    continue;
                }
                break;
            }
            return Integer.parseInt(size.trim());
        } catch (Exception ex) {
            logger.error("failed to find mem size", ex);
            throw ex;
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException ex1) {
                    logger.error("failed to close reader", ex1);
                }
            }
        }
    }
}
